package cn.thinkluck.pattern.state.mario.branch;

import cn.thinkluck.pattern.state.mario.Event;
import cn.thinkluck.pattern.state.mario.MarioState;
import lombok.Getter;

/**
 * 马里奥
 * 分支预测法马里奥状态
 *
 * @Author liutao
 * @DateTime 2024/8/3 下午4:42
 */
@Getter
public class Mario {
    /**
     * 马里奥状态
     */
    private MarioState state;

    /**
     * 初始化小马里奥
     */
    public Mario() {
        this.state = MarioState.SMALL;
    }

    /**
     * 触发事件
     * 分支预测法
     *
     * @param event 事件
     */
    public void handleEvent(Event event) {
        //如果是死马里奥，那么就不用处理事件了
        if (this.state == MarioState.DEAD) {
            return;
        }
        //吃蘑菇
        if (event == Event.MUSHROOM) {
            //只有小马里奥吃蘑菇才能变大
            if (this.state == MarioState.SMALL) {
                this.state = MarioState.BIG;
            }
        }
        //吃火花
        if (event == Event.FIRE_FLOWER) {
            //只有大马里奥才能吃火花才能变火焰马里奥
            if (this.state == MarioState.BIG) {
                this.state = MarioState.FIRE;
            }
        }
        //被攻击
        if (event == Event.ENEMY_ATTACK) {
            //如果是小马里奥，那么就是死
            if (this.state == MarioState.SMALL) {
                this.state = MarioState.DEAD;
            }
            //如果是大马里奥，那么就是变小
            if (this.state == MarioState.BIG) {
                this.state = MarioState.SMALL;
            }
            //如果是火焰马里奥，那么也是变小马里奥
            if (this.state == MarioState.FIRE) {
                this.state = MarioState.SMALL;
            }
        }
        //如果掉坑里，都会死
        if (event == Event.FALL_INTO_PIT) {
            this.state = MarioState.DEAD;
        }
    }
}
